package cn.com.freelibra.forum.web;

import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.freelibra.forum.po.SectionDetail;
import cn.com.freelibra.forum.po.User;
import cn.com.freelibra.forum.service.SectionDetailService;
import cn.com.freelibra.forum.service.SectionService;
import cn.com.freelibra.forum.util.HtmlUtil;
import cn.com.freelibra.forum.web.view.AjaxObject;

/**
 * 帖子
 * @author qing
 *
 */
@Controller
@RequestMapping("/sectionDetal")
public class SectionDetailController {
	
	@Resource
	private SectionService sectionService;
	@Resource
	private SectionDetailService sectionDetailService;
	
	/**
	 * 保存
	 * @param model
	 * @param sectionId
	 * @param code
	 * @param context
	 * @param sectionImg
	 * @param name
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxObject sectionDetailSave(Model model,String context,Long sectionId,HttpServletRequest request){
		AjaxObject ao = new AjaxObject();
		if(context==null||"".equals(context.trim())){
			ao.setStatus(401);//标题
			ao.setMsg("请输入内容！");//提示内容
			ao.setAction("alert");//弹窗提示
			return ao;
		}
		if(context.trim().length()<10){
			ao.setStatus(402);//标题
			ao.setMsg("内容不能少于10个字");//提示内容
			ao.setAction("alert");//弹窗提示
			return ao;
		}
		User user = (User) request.getSession().getAttribute("user");
		if(user==null){
			ao.setStatus(405);//标题
			ao.setMsg("登陆状态已过期，请重新登陆");//提示内容
			ao.setAction("alert");//弹窗提示
			return ao;
		}else{
			SectionDetail sectionDetail = new SectionDetail();
			sectionDetail.setContext(context);
			//sectionDetail.setReuserid();
			sectionDetail.setUserid(user.getId());
			sectionDetail.setCreateTime(new Date());
			sectionDetail.setItStatus(0);
			sectionDetail.setSid(sectionId);
			String text = HtmlUtil.removeHtmlInTag(context);
			if(text.length()>100)
				sectionDetail.setRemark(text.substring(0,100)+"...");
			else
				sectionDetail.setRemark(text.substring(0,text.length())+"...");
			sectionDetailService.add(sectionDetail);
			ao.setStatus(200);
			ao.setMsg("回帖成功");//提示内容
			ao.setAction("alert");//弹窗提示
			return ao;
		}
	}
}
